<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DefineProperty Multi</title>
</head>
<body>
    <div id="app">
        hello
    </div>
    <script>
        // 模拟 vue 中的 data 选项
        let data = {
            message: 'hello',
            count: 10
        }

        // 模拟 Vue 实例
        let vm = {}

        function proxyData(data){
            // 遍历 data 对象的所有属性 
            Object.keys(data).forEach(key=>{
                Object.defineProperty(vm, key, {
                    enumerable: true,
                    configurable: true,
                    get(){
                        console.log('get:', key, data[key])
                        return data[key]
                    },
                    set(newValue){
                        console.log('set:',key, newValue)
                        if(newValue === data[key]) return
                        data[key] = newValue,
                        document.querySelector('#app').textContent = data[key]
                    }
                    
                })
            })
        }

     
        proxyData(data)

        // 测试
        vm.message = 'Husky'
        console.log(vm.message)

    </script>
</body>
</html>